package com.dld.coupon.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dld.android.persistent.SharePersistent;
import com.dld.android.util.LogUtils;
import com.dld.android.view.SegmentControl;
import com.dld.android.view.SegmentControlAdapter;
import com.dld.coupon.util.Common;
import com.dld.coupon.util.MapUtil;
import com.dld.coupon.util.StringUtils;
import com.dld.coupon.view.BottomTabGroup;

public abstract class BaseActivity extends Activity {
	public static final int[] BUTTON_ID;
	public static final String[] BUTTON_TEXT;
	public static final String Base = "BaseActivity";
	public static final int[] FOCUS_IMAGE_ID;
	public static final int[] UNFOCUS_IMAGE_ID;
	public static int segmentPosition;

	static {

		FOCUS_IMAGE_ID = new int[] { R.drawable.index, R.drawable.around,
				R.drawable.discount, R.drawable.shopping,
				R.drawable.cheapskatecard };
		UNFOCUS_IMAGE_ID = new int[] { R.drawable.index_blue,
				R.drawable.around_blue, R.drawable.discount_blue,
				R.drawable.shopping_blue, R.drawable.cheapskatecard_blue };
		BUTTON_TEXT = new String[] { "首页", "周边", "打折", "街店", "小气鬼卡" };
		BUTTON_ID = new int[] { R.id.button_arround, R.id.button_recommand,
				R.id.button_more, R.id.button_group, R.id.button_store };

	}

	private View buildButton(int paramInt) {
		RelativeLayout localLinearLayout = (RelativeLayout) findViewById(BUTTON_ID[paramInt]);
		((ImageView) localLinearLayout.findViewById(R.id.image))
				.setImageDrawable(getUnfocusImageDrawable(paramInt));
		TextView localTextView = (TextView) localLinearLayout
				.findViewById(R.id.text);
		localTextView.setText(BUTTON_TEXT[paramInt]);
		return localLinearLayout;
	}

	private Drawable getFocusImageDrawable(int paramInt) {
		return getApplicationContext().getResources().getDrawable(
				FOCUS_IMAGE_ID[paramInt]);
	}

	private Drawable getUnfocusImageDrawable(int paramInt) {
		return getApplicationContext().getResources().getDrawable(
				UNFOCUS_IMAGE_ID[paramInt]);
	}

	public void finish() {
		super.finish();
		ActivityManager.pop();
	}

	protected void initSegment() {
		Object btg = (BottomTabGroup) findViewById(R.id.bottom_button_group);
		((SegmentControl) btg).setAdapter(new SegmentControlAdapter() {
			public boolean disable() {
				return false;
			}

			public int getCount() {
				return BaseActivity.BUTTON_TEXT.length;
			}

			public int getDefaultFocusIndex() {
				return segmentPosition;
			}

			public View getView(int paramInt) {
				return BaseActivity.this.buildButton(paramInt);
			}

			public void onBack() {
				Common.startActivity(MainActivity.class);
			}

			public void onBlur(View paramView, int paramInt) {
			}

			public void onFocus(View paramView, int paramInt) {
				BaseActivity.segmentPosition = paramInt;
				Common.startActivity(MainActivity.class);
			}
		});

		((BottomTabGroup) btg).changeFocus(segmentPosition);
		btg = ((BottomTabGroup) btg).findViewById(BUTTON_ID[segmentPosition]);
		((ImageView) ((View) btg).findViewById(R.id.image))
				.setImageDrawable(getFocusImageDrawable(segmentPosition));
		((TextView) ((View) btg).findViewById(R.id.text))
				.setTextColor(R.color.white);
	}

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		requestWindowFeature(1);
		setRequestedOrientation(1);
		ActivityManager.push(this);
	}

	public boolean onCreateOptionsMenu(Menu paramMenu) {
		super.onCreateOptionsMenu(paramMenu);
		// paramMenu.add(0, 0, 0, "设置").setIcon(R.drawable.menu_setting);
		paramMenu.add(0, 1, 1, "城市设置").setIcon(R.drawable.changecity);
		paramMenu.add(0, 3, 3, "客服电话").setIcon(R.drawable.phone);
		// paramMenu.add(0, 4, 4, "意见反馈").setIcon(R.drawable.feedback);
		paramMenu.add(0, 5, 5, "关于").setIcon(R.drawable.about);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem paramMenuItem) {
		super.onOptionsItemSelected(paramMenuItem);
		Object localObject = new Intent();
		switch (paramMenuItem.getItemId()) {
		// case 0:
		// Toast.makeText(ActivityManager.getCurrent(), "咱不支持此功能！", 0).show();
		// Common.startActivity(MainActivity.class);
		// BaseActivity.segmentPosition = 4;
		// break;
		case 1:
			((Intent) localObject).setClass(ActivityManager.getCurrent(),
					CityActivity.class);
			ActivityManager.getCurrent().startActivity((Intent) localObject);
			break;
		case 2:
			BaseActivity.segmentPosition = 2;
			ActivityManager.back();
			try {
				if (ActivityManager.getCurrent().getParent() instanceof MainActivity) {
					MainActivity main = (MainActivity) ActivityManager
							.getCurrent().getParent();
					main.setCurrentTab(2);
				}
			} catch (Exception e) {
				e.printStackTrace();

			}
			break;
		case 3:
			localObject = new Intent("android.intent.action.DIAL");
			((Intent) localObject).setData(Uri.parse("tel:4000088114"));
			ActivityManager.getCurrent().startActivity((Intent) localObject);
			break;
		/**
		 * case 4: localObject = new Intent(); ((Intent)
		 * localObject).setClass(ActivityManager.getCurrent(),
		 * FeedbackActivity.class);
		 * ActivityManager.getCurrent().startActivity((Intent) localObject);
		 * break;
		 */
		case 5:
			Builder dialog = new AlertDialog.Builder(ActivityManager
					.getCurrent());
			dialog.setTitle("关于");
			dialog.setMessage("店连店打折网\n\n版本 1.0\n\n网址: www.dld.com");
			dialog.setPositiveButton("返回",
					new DialogInterface.OnClickListener() {
						public void onClick(
								DialogInterface paramDialogInterface,
								int paramInt) {
						}
					});
			dialog.show();
		}
		return true;
	}

	public boolean onPrepareOptionsMenu(Menu paramMenu) {
		paramMenu.removeItem(2);
		if (!StringUtils.isEmpty(SharePersistent.get("customer_id")))
			paramMenu.add(0, 2, 2, "退出登录").setIcon(2130837650);
		else
			paramMenu.add(0, 2, 2, "登录").setIcon(2130837648);
		return true;
	}

	protected void onResume() {
		super.onResume();
		LogUtils.log("BaseActivity", "onresume ActivityManager.add(this) ");
		ActivityManager.push(this);
		initAboutus();
		intret();
	}

	private void intret() {
		Button btnretn = (Button) findViewById(R.id.back_top_btn);
		if (btnretn == null) {
			return;
		}
		btnretn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (getIntent() != null && getIntent().getExtras() != null) {
					int pos = getIntent().getExtras().getInt("previous_pos");
					BaseActivity.segmentPosition = pos;
				}
				ActivityManager.back();
			}

		});

	}

	private void initAboutus() {
		Button btn = (Button) findViewById(R.id.aboutus_top_btn);

		if (btn == null) {
			return;
		}
		btn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent it = new Intent(ActivityManager.getCurrent(),
						AboutUs.class);
				it.putExtra("previous_pos", BaseActivity.segmentPosition);
				BaseActivity.segmentPosition = 4;
				startActivity(it);
			}

		});
	}

	@Override
	protected void onStop() {
		super.onStop();

	}

	@Override
	public void onBackPressed() {
		Log.v("", "*****************  onbacked pressed  "
				+ ActivityManager.getSize() + " : "
				+ (this instanceof AroundActivity));

		if (ActivityManager.getSize() == 1 || (this instanceof AroundActivity)) {
			AlertDialog ad = new AlertDialog.Builder(this).setTitle("提示")
					.setMessage("是否退出店连店打折网？").setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(
										DialogInterface paramDialogInterface,
										int paramInt) {
									BaseActivity.this.finish();
									MapUtil.destoryMap();
								}
							}).setNegativeButton("返回",
							new DialogInterface.OnClickListener() {
								public void onClick(
										DialogInterface paramDialogInterface,
										int paramInt) {
								}
							}).create();
			ad.show();
		} else {
			ActivityManager.back();
		}

	}

}
